<?php
namespace module\manage\controller\auth;

use app\Controller;
use app\model\auth\Perm as Model;

/**
 * 系统权限管理控制器
 */
class Perm extends Controller
{
    /**
     * 列表.
     * 
     */
    public function index(){
        $result = Model::with('children')->withSearch(['status'], input('get.'))->where('pid', 0)->paginate([
            'list_rows'=> input('get.pageSize/d', 10),
        ]);
        return json([
            'items' => $result->items(),
            'total' => $result->total()
        ]);
    }
    /**
     * 读取.
     * 
     */
    public function read(int $id){
        $model = Model::findOrFail($id);
        return ['view' => $model->toArray()];
    }
    /**
     * 保存.
     * 
     */
    public function save()
    {
        $model = new Model;
        $model->save($this->request->post());
        return json($model->toArray());
    }
    /**
     * 更新.
     * 
     * @param int $id
     */
    public function update(int $id)
    {
        $model = Model::findOrFail($id);
        $model->save($this->request->put());
        return json($model->toArray());
    }
    /**
     * 删除.
     * 
     * @param int $id
     */
    public function del(int $id)
    {
        $model = Model::findOrFail($id);
        $model->delete();
        return json($model->toArray());
    }
    /**
     * 保存.
     * 
     */
    public function tree()
    {
        return json(Model::tree(['value' => 'code', 'label' => 'name', 'pid' => 'pid']));
    }
}
